package mine.code.day.year2021.month10;

import org.junit.Test;

/**
 * 将非负整数 num 转换为其对应的英文表示。
 * <p>
 * 示例 1：
 * <p>
 * 输入：num = 123
 * 输出："One Hundred Twenty Three"
 *
 * @author caijinnan
 */
public class Day11_整数转换英文表示 {

    @Test
    public void run() {
        System.out.println(numberToWords(100));
    }

    String[] numArr = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
    String[] tenNumArr = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    String[] tenBitNumArr = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};

    public String numberToWords(int num) {
        String numStr = String.valueOf(num);
        int length = numStr.length();
        if (length >= 10) {
            String result = numberToWords(num / 1000000000) + " Billion " + numberToWords(num % 1000000000);
            return result.replaceAll(" Zero","");
        } else if (length >= 7) {
            String result = numberToWords(num / 1000000) + " Million " + numberToWords(num % 1000000);
            return result.replaceAll(" Zero","");
        } else if (length >= 4) {
            String result = numberToWords(num / 1000) + " Thousand " + numberToWords(num % 1000);
            return result.replaceAll(" Zero","");
        } else if (length >= 3) {
            String result = numberToWords(num / 100) + " Hundred " + numberToWords(num % 100);
            return result.replaceAll(" Zero","");
        } else if (length == 2) {
            if (num < 20) {
                return tenNumArr[num - 10];
            }
            if (num % 10!=0) {
                return tenBitNumArr[num / 10] + " " + numberToWords(num % 10);
            }
            return tenBitNumArr[num / 10];
        } else {
            return numArr[num];
        }
    }
}
